package org.huangrui.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author hr
 * @Create 2025-11-11 8:11
 **/
@RestController
public class ChatClientController {
    private final ChatClient chatClient;
    /**
     * 构造函数 注入
     */
    public ChatClientController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }
    @Autowired
    private ChatClient chatClientAi;
    // http://localhost:8001/chat
    @GetMapping("/chat")
    public String chat(@RequestParam (value = "msg", defaultValue = "你是谁") String msg) {
        return chatClient.prompt()
                // 用户输入
                .user( msg)
                // 调用大模型
                .call()
                .content();
    }
    @GetMapping("/chatAi")
    public String chatAi(@RequestParam (value = "msg", defaultValue = "你是谁") String msg) {
        return chatClientAi.prompt()
                .user(msg)
                .call()
                .content();
    }
    // 流式接口
    @GetMapping(value = "/chat/stream",produces="text/html;charset=UTF-8")
    public Flux<String> chatStream(@RequestParam(value = "msg", defaultValue = "请问你是谁") String message) {
        return chatClientAi.prompt().user(message).stream().content();
    }
}
